﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccessLayer;

namespace BusinessLogicLayer
{
    public class CongTacBLL
    {
        QuanLyChamCongDataContext DB = new QuanLyChamCongDataContext();
        public List<PHEPCONGTAC> DSDonXinPhepCT()
        {
            return DB.PHEPCONGTACs.ToList();
        }
        public void TaoDonXinPhepCT(PHEPCONGTAC pct)
        {
            DB.PHEPCONGTACs.InsertOnSubmit(pct);
            DB.SubmitChanges();
        }
        public List<PHEPCONGTAC> ThongTinPhepCT(int _MaPhepCT, int _MaPB)
        {
            return DB.PHEPCONGTACs.Where(mp => mp.MaPhepCT == _MaPhepCT && mp.NHANVIEN.MaPB == _MaPB).ToList();
        }
        public void DuyetDonXinPhep(PHEPCONGTAC _pct)
        {
            PHEPCONGTAC pct = DB.PHEPCONGTACs.Single(p => p.MaPhepCT == _pct.MaPhepCT);
            pct.TrangThaiDuyet = 1;
            pct.ChuThich = _pct.ChuThich;
            pct.NguoiDuyet = _pct.NguoiDuyet;
            DB.SubmitChanges();
        }
        public void KhongDuyetDonXinPhep(PHEPCONGTAC _pct)
        {
            PHEPCONGTAC pct = DB.PHEPCONGTACs.Single(p => p.MaPhepCT == _pct.MaPhepCT);
            pct.TrangThaiDuyet = 2;
            pct.ChuThich = _pct.ChuThich;
            pct.NguoiDuyet = _pct.NguoiDuyet;
            DB.SubmitChanges();
        }

        public List<DGVPHEPCONGTAC> DSPhepCaNhan(int _MaNV, int _Context)
        {
            //_Context : trạng thái duyệt
            List<DGVPHEPCONGTAC> ls = (from a in DB.PHEPCONGTACs
                                       orderby a.MaPhepCT descending
                                       where a.MaNV == _MaNV && a.TrangThaiDuyet == _Context
                                       select new DGVPHEPCONGTAC()
                                       {
                                           MaPhepCT = a.MaPhepCT,
                                           MaNV = a.MaNV ?? int.MaxValue,
                                           HoTen = a.NHANVIEN.HoTen,
                                           ChiTiet = a.ChiTiet,
                                           NgayDi = a.NgayDi ?? DateTime.MaxValue,
                                           NgayLamViecLai = a.NgayLamViecLai ?? DateTime.MaxValue,
                                           SoNgayCT = a.SoNgayCT ?? float.MaxValue,
                                           NguoiDuyet = a.NguoiDuyet ?? int.MaxValue,
                                           TrangThaiDuyet = a.TrangThaiDuyet ?? int.MaxValue,
                                           ChuThich = a.ChuThich,
                                           NgayTao = a.NgayTao ?? DateTime.MaxValue,
                                           NgayDuyet = a.NgayDuyet ?? DateTime.MaxValue,
                                           TrangThaiDoc = a.TrangThaiDoc ?? 1
                                       }).ToList();
            return ls;
        }

        public List<PHEPCONGTAC> LayThongTinPhepCT(int mp)
        {
            return DB.PHEPCONGTACs.Where(a => a.MaPhepCT == mp).ToList();
        }

        public string LayChuThich(int mp)
        {
            PHEPCONGTAC p = DB.PHEPCONGTACs.SingleOrDefault(a => a.MaPhepCT == mp);
            if (p.ChuThich != null)
            {
                return p.ChuThich.ToString();
            }
            else
            {
                return "";
            }
        }

        public int DemThongBao(int _maNV, int _maPB, int _mattd)
        {
            if (_maPB == 0)
            {
                List<PHEPCONGTAC> ls = (from a in DB.PHEPCONGTACs
                                        where a.MaNV == _maNV && a.TrangThaiDuyet == _mattd && a.TrangThaiDoc == 0
                                        select a).ToList();
                if (ls != null)
                {
                    return ls.Count;
                }
                else
                    return 0;
            }
            else
            {
                List<PHEPCONGTAC> ls = (from a in DB.PHEPCONGTACs
                                        where a.NHANVIEN.MaPB == _maPB && a.TrangThaiDuyet == _mattd
                                        select a).ToList();
                if (ls != null)
                {
                    return ls.Count;
                }
                else
                    return 0;
            }
        }

        public void Sua(PHEPCONGTAC _P)
        {
            PHEPCONGTAC P = DB.PHEPCONGTACs.SingleOrDefault(a => a.MaPhepCT == _P.MaPhepCT);
            if (P != null)
            {
                P.TrangThaiDoc = _P.TrangThaiDoc;
                DB.SubmitChanges();
            }
        }
    }
}
